package com.mutouren.modules.org.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.mutouren.common.entity.PageInfo;
import com.mutouren.common.orm.spring.BaseServiceImpl;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.dao.OrganizationRoleDao;
import com.mutouren.modules.org.dao.RoleDao;
import com.mutouren.modules.org.dao.RoleFunctionDao;
import com.mutouren.modules.org.model.Function;
import com.mutouren.modules.org.model.Organization;
import com.mutouren.modules.org.model.OrganizationRole;
import com.mutouren.modules.org.model.Role;
import com.mutouren.modules.org.model.RoleFunction;
import com.mutouren.modules.org.service.OrganizationService;
import com.mutouren.modules.org.service.RoleService;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
	
	@Resource 
	private RoleDao roleDao;
	
	@Resource
	private OrganizationService organizationService;		
	
	@Resource 
	private OrganizationRoleDao organizationRoleDao;	
	
	@Resource 
	private RoleFunctionDao roleFunctionDao;	

	@Override
	public void afterPropertiesSet() throws Exception {
		this.baseDao = roleDao;		
	}

	@Override
	public int addToOrg(int orgId, int roleId) {
		if (this.organizationRoleDao.get(new OrganizationRole(orgId, roleId)) == null) {
			this.organizationRoleDao.insert(new OrganizationRole(orgId, roleId));
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public void removeFromOrg(int orgId, int roleId) {
		this.organizationRoleDao.delete(new OrganizationRole(orgId, roleId));
	}

	@Override
	public void addFunction(int orgId, int funcId) {
		this.roleFunctionDao.insert(new RoleFunction(orgId, funcId));		
	}

	@Override
	public void deleteFunction(int orgId, int funcId) {
		this.roleFunctionDao.delete(new RoleFunction(orgId, funcId));		
	}

	@Override
	public List<Role> selectByAppId(int appId) {
		return this.roleDao.selectByAppId(appId);
	}

	@Override
	@Transactional
	public void setFunctions(int roleId, List<Integer> listFuncId) {
		this.roleFunctionDao.deleteByRoleId(roleId);
		
		for(Integer funcId : listFuncId) {
			this.roleFunctionDao.insert(new RoleFunction(roleId, funcId));
		}		
	}

	@Override
	public List<Function> getFunctions(int roleId) {
		return this.roleFunctionDao.getFunctions(roleId);
	}

	@Override
	public List<Role> selectByOrgId(int orgId, boolean isInherit, PageInfo pageInfo) {
		
		List<Integer> ids = null;
		
		if (isInherit) {
			Organization org = organizationService.get(orgId);
			ids = StringUtils.toListInteger(org.getSelfIdPath(), Organization.PATH_SEPARATOR);
		} else {
			ids = new ArrayList<Integer>();
			ids.add(orgId);
		}
		
		PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);		
		List<Role> result = this.organizationRoleDao.selectRoleByOrgIds(ids);
		
		com.github.pagehelper.Page<Role> page = (com.github.pagehelper.Page<Role>)result;		
		pageInfo.setTotal((int)page.getTotal());		
		
		return result;
	}

}
